﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using webapi.Models;
using System.Web.Security;

namespace webapi.views
{
  public partial class login : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_login_Click(object sender, EventArgs e)
    {
      string id = stuId.Text.Trim();
      string password = pwd.Text.Trim();
      if (id == "" || password == "")
      {
        this.showDialog("用户名或密码不能为空!");
      }
      else
      {
        string sql = "select * from [user] where user_id like '" + id + "'";
        DataSet ds = DB.ExecuteSelectSql(sql);

        //判断用户是否存在
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 && !ds.Tables[0].Rows[0].IsNull(0))
        {
          string key = ds.Tables[0].Rows[0][2].ToString();
          string type = ds.Tables[0].Rows[0][5].ToString();
          //MD5加密
          string md = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
          if (md == key)
          {
            HttpCookie cookie = new HttpCookie("user_id");
            cookie.Value = id;
            cookie.Expires = DateTime.Now.AddSeconds(3);
            Response.Cookies.Add(cookie);
            string url = "student.aspx";
            if (type == "1")
            {
              url = "admin.aspx";
            }
            Response.Redirect(url);
          }
          else
          {
            this.showDialog("账号或密码错误");
          }
        }
        else  //不存在该用户
        {
          this.showDialog("不存在该用户");
        }
      }
    }

    protected void btn_empty_Click(object sender, EventArgs e)
    {
      stuId.Text = "";
      pwd.Text = "";
    }

    protected void showDialog(string txt)
    {
      string str = "<script language='javascript'>alert('" + txt + "')</script>";
      Response.Write(str);
    }
  }
}